package com.anuode.core.controll;

import android.app.Application;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.anuode.bitmap.BitmapInterface;
import com.anuode.bitmap.CBitmap;
import com.anuode.db.CFrameDb;
import com.anuode.http.volley.VolleyHttpUtils;
import com.anuode.http.volley.bitmap.VolleyBitmap;
import com.anuode.http.volley.cache.CFrameVolley;
import com.anuode.util.Logs;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.volley.VolleyDraweeControllerBuilderSupplier;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.Map;


/**
 * @author cxd
 */
public abstract class AppContextControll extends Application implements
        Thread.UncaughtExceptionHandler {
    // public static KennerControll controll;
    public Map<Object, Object> cache;
    public static AppContextControll app;

    @Override
    public abstract void uncaughtException(Thread thread, Throwable ex);

    @Override
    public void onCreate() {
        super.onCreate();
        Logs.isShowLog = true;
        Logs.i("AppContextControll.onCreate() === > ", "......");
        app = this;
        Thread.setDefaultUncaughtExceptionHandler(this);
        init();

    }

    public static void init() {


        try {
            // TODO
            app.cache = new HashMap<>();
            KennerControll kennerControll = KennerControll.getInstance();
            kennerControll.setListener(new SimpleTaskListener());
            kennerControll.setCtx(app);
            app.cache.put("KennerControll", kennerControll);

            RequestQueue queue = CFrameVolley.newRequestQueue(app);
            app.cache.put("RequestQueue", queue);

            VolleyHttpUtils httpUtils = VolleyHttpUtils.getInstance(queue);
            app.cache.put("httpUtils", httpUtils);

            CFrameDb db = CFrameDb.create(app, "wetrip", true);
            app.cache.put("db", db);
            //m默认是 VolleyBitmap  可以手动切换到 CBitmap
            int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 12);
            //1/12允许内存作为缓存
            Logs.w("允许最大缓存内存：", cacheSize / 1024 / 1024 + " M");
            VolleyBitmap bitmapInterface = new VolleyBitmap(cacheSize, app, queue);
            app.cache.put("bitmap", bitmapInterface);

            ImageLoader imageLoader = bitmapInterface.getLoader(); // build yourself
            VolleyDraweeControllerBuilderSupplier mControllerBuilderSupplier
                    = new VolleyDraweeControllerBuilderSupplier(app, imageLoader);
            SimpleDraweeView.initialize(mControllerBuilderSupplier);
            Fresco.initialize(app);
            app.cache.put("Supplier", mControllerBuilderSupplier);
            CBitmap cBitmap = CBitmap.create(AppContextControll.app);

            app.cache.put("cBitmap", cBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onLowMemory() {
        try {
            Logs.w("AppContextControll.onLowMemory() === > ", "onLowMemory:");
            BitmapInterface bitmapInterface = (BitmapInterface) cache.get("bitmap");
            bitmapInterface.free();

            CBitmap cBitmap = (CBitmap)app.cache.get("cBitmap");
            cBitmap.free();

            CFrameVolley.free();
            System.gc();
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onLowMemory();
    }
}
